package com.lujieni.nio.c1;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

/**
 * transferTo 对于2g以上的文件会有问题,因此需要循环读
 */
public class TestFileChannelTransferTo {

    public static void main(String[] args) {
        try (FileChannel from = new FileInputStream("a.mkv").getChannel();
             FileChannel to = new FileOutputStream("b.mkv").getChannel()
        ) {
            long size = from.size(); //每次读取的数量
            long position = 0; //开始读取的下标
            while(true){
                long hasReadCount = from.transferTo(position, size, to);
                if(hasReadCount == 0){
                    return;
                }
                position = position + hasReadCount;
                size = size - hasReadCount;
            }
        } catch (IOException e) {
        }

    }


}
